package com.study.suanfa.shu.erchashu;

/**
 * @description: ShunXuCunChuBinaryTree  顺序存储二叉树
 * @date: 2020/12/4 17:02
 * @author: hcm
 * @version: 1.0
 */
public class ShunXuCunChuBinaryTree {
    public static void main(String[] args) {
        int[] arr = {0,1,4,3,8};
        ArrBinaryTree arrBinaryTree = new ArrBinaryTree(arr);
        arrBinaryTree.preOrder();
    }

}

/**
 * 顺序存储二叉树
 */
class ArrBinaryTree{
    private  int[] arr;

    public ArrBinaryTree(int[] arr) {
        this.arr = arr;
    }


    /**
     * 前序遍历
     */
    public void  preOrder(int rootIndex){
        if(arr.length == 0 || arr == null){
            System.out.println("数组为空无法遍历");
            return;
        }
        //前序遍历，先输出根节点
        System.out.println(arr[rootIndex]);
        //左递归
        if(rootIndex*2+1<arr.length){
            preOrder(rootIndex*2+1);
        }
        //右递归
        if(rootIndex*2+2<arr.length){
            preOrder(rootIndex*2+2);
        }
    }


    public void preOrder() {
        this.preOrder(0);
    }
}
